<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <!-- 区间子数组个数 -->
    <script>
      var numSubarrayBoundedMax = function (nums, left, right) {
        var cnt = max => {
          // 如何计算最大值小于等于max的子数组个数?
          // 枚举以每个数字结尾的子数组
          //                                  最大值              满足的子数组个数
          // 以2结尾的子数组    2                  1                     1
          // 以1结尾的子数组    2 21               2                     1+2
          // 以4结尾的子数组    4 14 214           4
          // 以3结尾的子数组    3 43 143 2143      4
          let cur = 0
          let res = 0
          for (let x of nums) {
            if (x <= max) {
              res += ++cur
            } else cur = 0
          }
          return res
        }
        return cnt(right) - cnt(left - 1)
      }
      console.log(numSubarrayBoundedMax([2, 1, 4, 3], 2, 3))
    </script>
  </body>
</html>
